home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / irisgl_vs_opengl / edges.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.5 KB  |  160 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* edges.c - open a window, clear the background, and render
  19.  *      a non-convex polygon using triangles.  Set the edge flag
  20.  *    to false for nonboundary edges.
  21.  *
  22.  *    <e> Key        - toggle inner edge on/off
  23.  *    Escape key    - exit the program
  24.  */
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28.  
  29. #include <math.h>
  30. #include <stdio.h>
  31.  
  32. /* Function Prototypes */
  33.  
  34. GLvoid  initgfx( GLvoid );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38.  
  39. void printHelp( char * );
  40.  
  41. /* Global Definitions */
  42.  
  43. #define KEY_ESC    27    /* ascii value for the escape key */
  44.  
  45. /* Global Variables */
  46.  
  47. static GLboolean showEdges = GL_FALSE;
  48.  
  49. void
  50. main( int argc, char *argv[] )
  51. {
  52.     GLsizei     width, height;
  53.  
  54.     glutInit( &argc, argv );
  55.  
  56.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  57.     height = glutGet( GLUT_SCREEN_HEIGHT );
  58.     glutInitWindowPosition( width/4, height/4 ); 
  59.     glutInitWindowSize( width/2, height/2 );
  60.     glutInitDisplayMode( GLUT_RGBA );
  61.     glutCreateWindow( argv[0] );
  62.     
  63.     initgfx();
  64.  
  65.     glutKeyboardFunc( keyboard );
  66.     glutReshapeFunc( reshape );
  67.     glutDisplayFunc( drawScene ); 
  68.  
  69.     printHelp( argv[0] );
  70.  
  71.     glutMainLoop();
  72. }
  73.  
  74. GLvoid
  75. printHelp( char *progname )
  76. {
  77.     fprintf(stdout,
  78.         "\n%s - demonstrates how to set the edge flag so that\n"
  79.         "non-boundary edges are not rendered\n\n"
  80.         "<e> Key        - toggle edge flags on/off\n"
  81.         "Escape key        - exit the program\n\n",
  82.         progname
  83.     );
  84. }
  85.  
  86. GLvoid
  87. initgfx( GLvoid )
  88. {
  89.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  90. }
  91.  
  92. GLvoid 
  93. keyboard( GLubyte key, GLint x, GLint y )
  94. {
  95.     switch (key) {
  96.     case 'e':
  97.         showEdges = !showEdges;
  98.         glutPostRedisplay();
  99.         break;
  100.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  101.         exit(0);
  102.     }
  103. }
  104.  
  105. GLvoid
  106. reshape( GLsizei width, GLsizei height )
  107. {
  108.     GLdouble    aspect;
  109.  
  110.     glViewport( 0, 0, width, height );
  111.  
  112.     aspect = (GLdouble) width / (GLdouble) height;
  113.  
  114.     glMatrixMode( GL_PROJECTION );
  115.     glLoadIdentity();
  116.         glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  117.     glMatrixMode( GL_MODELVIEW );
  118.     glLoadIdentity();
  119. }
  120.  
  121. GLvoid
  122. drawScene( GLvoid )
  123. {
  124.     static GLfloat v0[2] = { -1.0, 1.0 };
  125.     static GLfloat v1[2] = { -1.0, -1.0 };
  126.     static GLfloat v2[2] = { 1.0, -1.0};
  127.     static GLfloat v3[2] = { 1.0, 1.0 };
  128.     static GLfloat v4[2] = { 0.0, 0.0 };
  129.     
  130.     glClear( GL_COLOR_BUFFER_BIT );
  131.  
  132.     glPolygonMode( GL_FRONT, GL_LINE ); 
  133.     glLineWidth( 2.5 );    /* make lines really wide */
  134.     glColor3f( 1.0, 1.0, 1.0 );
  135.  
  136.     /* Draw a nonconvex polygon by breaking it up into three triangles */
  137.     glBegin( GL_TRIANGLES );
  138.         /* glEdgeFlag is initially true */
  139.         glVertex2fv( v0 );
  140.         if (!showEdges)
  141.             glEdgeFlag( GL_FALSE );
  142.         glVertex2fv( v1 );
  143.         glEdgeFlag( GL_TRUE );
  144.         glVertex2fv( v4 );
  145.  
  146.         glVertex2fv( v1 );
  147.         if (!showEdges)
  148.             glEdgeFlag( GL_FALSE );
  149.         glVertex2fv( v2 );
  150.         glVertex2fv( v4 );
  151.  
  152.         glVertex2fv( v4 );
  153.         glEdgeFlag( GL_TRUE );
  154.         glVertex2fv( v2 );
  155.         glVertex2fv( v3 );
  156.     glEnd();
  157.  
  158.     glFlush();
  159. }
  160.